package com.jxs.android.sxyw;

import java.io.IOException;
import java.util.Date;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.TaskInfo;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

public class InspectionSubmit extends AbstractActivity implements OnClickListener {

	private static final int REQUEST_CODE_GET_LOACTION = 0x881;
	private static final int REQUEST_CODE_GET_DECRIPTON = 0x882;
	private String latitude = "0.0";
	private String longitude = "0.0";
	private String desc = "";
	private int position = 0;

	private TextView xunjian_name_display, xunjian_plan_person_display, xunjian_get_location_display, xunjian_desc_display, xunjian_submit_person_display, xunjian_submit_time_display;
	private Button xunjian_sumbit;
	private TaskInfo taskInfo = null;
	private Context context;

	private LinearLayout xunjian_get_location, xunjian_desc;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.inspection_submit);

		context = this;

		xunjian_name_display = (TextView) findViewById(R.id.xunjian_name_display);
		xunjian_plan_person_display = (TextView) findViewById(R.id.xunjian_plan_person_display);
		xunjian_get_location_display = (TextView) findViewById(R.id.xunjian_get_location_display);
		xunjian_desc_display = (TextView) findViewById(R.id.xunjian_desc_display);
		xunjian_submit_person_display = (TextView) findViewById(R.id.xunjian_submit_person_display);
		xunjian_submit_time_display = (TextView) findViewById(R.id.xunjian_submit_time_display);
		xunjian_sumbit = (Button) findViewById(R.id.xunjian_sumbit);
		xunjian_get_location = (LinearLayout) findViewById(R.id.xunjian_get_location);
		xunjian_desc = (LinearLayout) findViewById(R.id.xunjian_desc);

		if (null != getIntent() && null != getIntent().getExtras()) {
			taskInfo = (TaskInfo) getIntent().getExtras().getSerializable("taskInfo");
			position = getIntent().getExtras().getInt("position");
		}

		if (null != taskInfo) {
			xunjian_name_display.setText(taskInfo.getSchedule_name());
			xunjian_plan_person_display.setText(taskInfo.getPlan_user());
			xunjian_submit_person_display.setText(SelfApplication.getUserInfo().getUser_name());
			xunjian_submit_time_display.setText(SDF_YYYY_MM_dd_HH_mm.format(new Date()));
		}

		xunjian_sumbit.setOnClickListener(this);
		xunjian_get_location.setOnClickListener(this);
		xunjian_desc.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == xunjian_sumbit.getId()) {

			if (StringUtils.isEmpety(latitude) || StringUtils.isEmpety(longitude) || "0.0".equals(latitude) || "0.0".equals(longitude)) {
				PopMsg.show("请获取经纬度");
			} else if (StringUtils.isEmpety(desc)) {
				PopMsg.show("请填写描述信息");
			} else {
				if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
					PopMsg.show("登录已过期,请重新登录");
					StartHelper.startActivity(Login.class);
				} else {
					sumbit();
				}
			}

		} else if (v.getId() == xunjian_get_location.getId()) {
			Intent intent = new Intent(context, StandardStatecraft.class);
			startActivityForResult(intent, REQUEST_CODE_GET_LOACTION);
		} else if (v.getId() == xunjian_desc.getId()) {
			Intent intent = new Intent(context, DescriptionSubmit.class);
			Bundle bundle = new Bundle();
			bundle.putString("desc", desc);
			bundle.putString("title", "描述信息");
			bundle.putInt("length", 300);
			intent.putExtras(bundle);
			startActivityForResult(intent, REQUEST_CODE_GET_DECRIPTON);
		}
	}

	/**
	 * @String ticket 用户ID（登陆成功时，系统返回的ticket）
	 * @String taskId 任务ID
	 * @String remark 描述
	 * @String longitude 经度
	 * @String latitude 纬度
	 */
	private void sumbit() {
		String wsdl_url = SelfApplication.WSDL_URL;// wsdl
		String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
		String methodName = "dealTask";// 要调用的方法名称
		SoapObject request = new SoapObject(namespace, methodName);
		request.addProperty("ticket", SelfApplication.getUserInfo().getTicket());
		request.addProperty("taskId", taskInfo.getTask_id());
		request.addProperty("remark", desc);
		request.addProperty("longitude", longitude);
		request.addProperty("latitude", latitude);
		BuWebService webService = new BuWebService(wsdl_url, request);
		webService.setDoNet(false);
		webService.requestWSDL(new IWebService() {
			@Override
			public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
				paser(response);
			}
		});
	}

	protected void paser(SoapPrimitive soapPrimitive) {
		if (null != soapPrimitive) {
			if ("0".equals(soapPrimitive.toString())) {
				handler.sendEmptyMessage(0);
			} else {
				handler.sendEmptyMessage(1);
			}
		}
	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				PopMsg.show("巡检失败！");
			} else if (msg.what == 1) {
				PopMsg.show("巡检成功！");
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putBoolean("success", true);
				bundle.putInt("position", position);
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				InspectionSubmit.this.finish();
			}
		}

	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUEST_CODE_GET_LOACTION && resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			this.longitude = bundle.getString("longitude");
			this.latitude = bundle.getString("latitude");
			xunjian_get_location_display.setText(latitude + "," + longitude);
		} else if (requestCode == REQUEST_CODE_GET_DECRIPTON && resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			desc = bundle.getString("desc");
			xunjian_desc_display.setText(desc);
		}
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.hindBtn();
		topBar.setTitle(R.string.xunjian_submit_title_label);
		initTopBarReturn(topBar);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

}
